package com.huixue.cn.ztej.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.util.StringUtil;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.TOrderInfo;
import com.huixue.cn.ztej.bean.User;
import com.huixue.cn.ztej.persistence.Domain;
import com.huixue.cn.ztej.service.TOrderService;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.framwork.JacksonUtil;
import com.huixue.cn.ztej.utils.framwork.PageParameter;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.Model;

@Domain(TOrderInfo.class)
@Controller
@RequestMapping(value = "/sys/order")
public class TOrderController extends BaseController {
	
	@Autowired(required = true)
	private TOrderService tOrderService;
	
	@RequestMapping(value = "/order_list")
	public String list(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = PageParameter.DEFAULT_PAGE_SIZE+"") Integer rows,
			TOrderInfo searchOrderForm,Model model,HttpServletRequest request) {
		
		Map<String,Object> searchParams = new HashMap<String,Object>();
		if(StringUtil.isNotEmpty(searchOrderForm.getOrderCode())){
			searchParams.put("orderCode", searchOrderForm.getOrderCode());
		}

		if(StringUtil.isNotEmpty(searchOrderForm.getSubjectId())){
			searchParams.put("subjectId", searchOrderForm.getSubjectId());
		}
		
		String startDate = request.getParameter("startDate");
		if(StringUtil.isNotEmpty(startDate)){
			searchParams.put("startDate", startDate);
			model.addAttribute("startDate", startDate);
		}
		
		String endDate = request.getParameter("endDate");
		if(StringUtil.isNotEmpty(endDate)){
			searchParams.put("endDate", endDate);
			model.addAttribute("endDate", endDate);
		}
		
		String status = request.getParameter("status");
		if(StringUtil.isNotEmpty(status)){
			searchParams.put("status", status);
			model.addAttribute("status", status);
		}
		Results results = tOrderService.searchByPage(searchParams,page,rows);
		model.addAttribute("res", results);
		model.addAttribute("searchOrderForm", searchOrderForm);
		return "/order/order_list";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toAdd")
	public String toAdd(Model model) {
		return "/torder/add";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toUpdate/{id}")
	public String toUpate(@PathVariable ("id")Integer id,Model model,HttpServletRequest request) {
		TOrderInfo info = tOrderService.getInfo(id);
		model.addAttribute("roleRes", info);
		return "/torder/update";
	}
	
	
	/**
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/udpate",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results udpate(@RequestBody TOrderInfo  info,Model model,HttpServletRequest request) {
		tOrderService.update(info);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "修改成功！");
		return results;
	}

	@ResponseBody
	@RequestMapping(value = "/add",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results add(@RequestBody TOrderInfo info,HttpServletRequest request) {
		tOrderService.save(info);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "保存成功！");
		return results;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/delete/{orderId}",produces="application/json;charset=UTF-8",method=RequestMethod.DELETE)
	public Results delete(@PathVariable(value="orderId") String orderId ,HttpServletRequest request) {
		
		TOrderInfo orderInfo = tOrderService.getInfo(Integer.valueOf(orderId));
		orderInfo.setStatus(-100);
		orderInfo.setUpdateTime(new Date());
		User currentUser = (User)request.getSession().getAttribute("loginUser");
		orderInfo.setUpdateId(currentUser.getUser_id());
		tOrderService.update(orderInfo);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "删除成功！");
		logger.debug( new JacksonUtil().getJson(results));
		return results;
	}
	
}
